package bcel.cardcenter.bconnex.recon.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Service;

import bcel.cardcenter.bconnex.recon.entity.BconTxn;
import bcel.cardcenter.bconnex.recon.entity.Entity;
@Service("fileReaderService")
public class FileReaderServiceImp implements FileReaderService {
	private static final String SLOT = "([0-9]{1,3})";
	private static final String NUM	 = "^[0-9]{1,}$";
	private Pattern slotPattern;
	private Pattern numPattern;
	private Matcher matcher;
	private List<Entity> entities;
	private List<BconTxn> bconTxns;
	public void setEntities(List<Entity> entities){
		this.entities = entities;
	}
	@Override
	public List<Entity> getEntities(){
		return entities;
	}
	public void setBconTxns(List<BconTxn> bconTxns){
		this.bconTxns = bconTxns;
	}
	@Override
	public List<BconTxn> getBconTxns(){
		return bconTxns;
	}
	@Override
	public void fileReader(File file) throws Exception {
		BufferedReader bufferReader = new BufferedReader(new FileReader(file));
		String line = null;
		List<Entity> entities = new ArrayList<Entity>();
		while((line = bufferReader.readLine())!=null){
			String card	  = line.substring(18,35).trim();
			String proc	  = line.substring(51,58).trim();
			String acco	  = line.substring(83,102).trim();
			String term	  = line.substring(102,111).trim();
			String accq   = line.substring(124,131).trim();
			String amt	  = line.substring(192,200).trim();
			String rrn	  = line.substring(252,265).trim();
			Entity entity = new Entity();
			entity.setCard(card);
			entity.setProc(proc);
			entity.setAcco(acco);
			entity.setTerm(term);
			entity.setAccq(accq);
			entity.setAmt(Double.parseDouble(amt));
			entity.setRrn(rrn);
			entities.add(entity);
		}
		setEntities(entities);
	}
	@Override
	public void fileWriter(List<Entity> entities, File file) throws IOException{
		FileWriter fileWriter = new FileWriter(file, true);
		for(Entity entity : entities){
			String str = entity.toString();
			System.out.println(str);
			fileWriter.write(str);
		}
		fileWriter.close();
	}
	@Override
	public void verifyLogFileReader(File file, Date date) throws Exception {
		BufferedReader bufferReader = new BufferedReader(new FileReader(file));
		String line = null;
		List<BconTxn> bconTxns = new ArrayList<BconTxn>();
		slotPattern = Pattern.compile(SLOT);
		numPattern = Pattern.compile(NUM);
		while((line = bufferReader.readLine())!=null){
			if(line.trim().length() > 70){
				String[] token = line.split(" ");
				matcher = slotPattern.matcher(token[0]);
				String slot = null;
				if(matcher.find())
					slot = matcher.group().trim();
				String type = token[1];
				String proc = token[2];
				String card	= token[3];
				String res	= token[5];
				String time	= token[6];
				String amt	= token[7];
				String atmId= token[8].substring(12,18);
				String refer= token[9];
				String stan = token[10];
				String acqId= token[11];
				
				matcher = numPattern.matcher(amt.trim());
				
				if(!matcher.find())
					continue;
					
				BconTxn bconTxn = new BconTxn();
				bconTxn.setSlot(slot);
				bconTxn.setDate(date);
				bconTxn.setType(type);
				bconTxn.setProc(proc);
				bconTxn.setCard(card);
				bconTxn.setRes(res);
				bconTxn.setTime(time);
				bconTxn.setAmount(Double.parseDouble(amt)/100);
				bconTxn.setTermi(atmId);
				bconTxn.setRefer(refer);
				bconTxn.setStan(stan);
				bconTxn.setAcqId(acqId);
				bconTxns.add(bconTxn);
			}
		}
		setBconTxns(bconTxns);
	}
	@Override
	public void fileBconnexWriter(List<BconTxn> bconTxns, File file) throws Exception {
		FileWriter fileWriter = new FileWriter(file, true);
		for(BconTxn entity : bconTxns){
			String str = entity.toString();
			System.out.println(str);
			fileWriter.write(str);
		}
		fileWriter.close();
	}

}
